/*
 * DevicesSPIFlash.h
 *      Author: hongyun.he
 */

#ifndef DEVICESSPIFLASH_H_
#define DEVICESSPIFLASH_H_


#include "DevicesSPI.h"


#define SPI_FLASH_BASE_ADDR                 0x00000000UL
/* size: 64k Byte */
#define SPI_FLASH_BOOTLOADER_BACK_ADDR      (SPI_FLASH_BASE_ADDR + 1024 * (0))
/* size: 4k Byte */
#define SPI_FLASH_SYSTEM_DATA_ADDR          (SPI_FLASH_BASE_ADDR + 1024 * (64))
/* size: 4k Byte */
#define SPI_FLASH_USER_DATA_ADDR            (SPI_FLASH_BASE_ADDR + 1024 * (64 + 4))
/* size: 440k Byte */
#define SPI_FLASH_APP_BACK_ADDR             (SPI_FLASH_BASE_ADDR + 1024 * (64 + 4 + 4))
/* size: 512k Byte */
#define SPI_FLASH_OTA_ADDR                  (SPI_FLASH_BASE_ADDR + 1024 * (64 + 4 + 4 + 440))
/* size: 1024k Byte */
#define SPI_FLASH_FONT_ADDR                 (SPI_FLASH_BASE_ADDR + 1024 * (64 + 4 + 4 + 440 + 512))
/* size: 256k Byte */
#define SPI_FLASH_DESKTOP_ADDR              (SPI_FLASH_BASE_ADDR + 1024 * (64 + 4 + 4 + 440 + 512 + 1024))
/* size: 1024k Byte */
#define SPI_FLASH_NES_ROM_ADDR              (SPI_FLASH_BASE_ADDR + 1024 * (64 + 4 + 4 + 440 + 512 + 1024 + 256))


/* Reset Operations */
#define RESET_ENABLE_CMD                     0x66
#define RESET_MEMORY_CMD                     0x99

/* Identification Operations */
#define READ_ID_CMD                          0x9E
#define READ_ID_CMD2                         0x9F
#define MULTIPLE_IO_READ_ID_CMD              0xAF
#define READ_SERIAL_FLASH_DISCO_PARAM_CMD    0x5A

/* Read Operations */
#define READ_CMD                             0x03
#define FAST_READ_CMD                        0x0B
#define DUAL_OUT_FAST_READ_CMD               0x3B
#define DUAL_INOUT_FAST_READ_CMD             0xBB
#define QUAD_OUT_FAST_READ_CMD               0x6B
#define QUAD_INOUT_FAST_READ_CMD             0xEB

/* Write Operations */
#define WRITE_ENABLE_CMD                     0x06
#define WRITE_DISABLE_CMD                    0x04

/* Register Operations */
#define READ_STATUS_REG_CMD                  0x05
#define WRITE_STATUS_REG_CMD                 0x01

#define READ_LOCK_REG_CMD                    0xE8
#define WRITE_LOCK_REG_CMD                   0xE5

#define READ_FLAG_STATUS_REG_CMD             0x70
#define CLEAR_FLAG_STATUS_REG_CMD            0x50

#define READ_NONVOL_CFG_REG_CMD              0xB5
#define WRITE_NONVOL_CFG_REG_CMD             0xB1

#define READ_VOL_CFG_REG_CMD                 0x85
#define WRITE_VOL_CFG_REG_CMD                0x81

#define READ_ENHANCED_VOL_CFG_REG_CMD        0x65
#define WRITE_ENHANCED_VOL_CFG_REG_CMD       0x61

/* Program Operations */
#define PAGE_PROG_CMD                        0x02
#define DUAL_IN_FAST_PROG_CMD                0xA2
#define EXT_DUAL_IN_FAST_PROG_CMD            0xD2
#define QUAD_IN_FAST_PROG_CMD                0x32
#define EXT_QUAD_IN_FAST_PROG_CMD            0x12

/* Erase Operations */
#define SUBSECTOR_ERASE_CMD                  0x20
#define SECTOR_ERASE_CMD                     0xD8
#define BULK_ERASE_CMD                       0xC7

#define PROG_ERASE_RESUME_CMD                0x7A
#define PROG_ERASE_SUSPEND_CMD               0x75

/* One-Time Programmable Operations */
#define READ_OTP_ARRAY_CMD                   0x4B
#define PROG_OTP_ARRAY_CMD                   0x42


/* GD25Q32E厂商号 */
#define Manufacturer_ID_GD25Q32E    0xC8
/* 块大小 */
#define SPI_FLASH_BLOCK_SIZE        4096
/* 页大小 */
#define SPI_FLASH_PAGE_SIZE         256

/* 需要提供的SPI操作函数接口 */
#define SPI_FLASH_CS_ENABLE()       SPI1_NSS_SET_LOW()
#define SPI_FLASH_CS_DISABLE()      do{ SPI1_NSS_SET_HIGH(); vDelayUs(1); }while(0)
#define ucSPIxWriteReadByte         ucSPI1WriteReadByte
#define cSPIxWriteDatas             cSPI1WriteDatas
#define cSPIxReadDatas              cSPI1ReadDatas



void vSPIFlashInit(void);
int8_t cSPIFlashErases(uint32_t uiAddress);
int8_t cSPIFlashNoErasesWriteDatas(uint32_t uiAddress, const void *pvBuff, int32_t iLength);
int8_t cSPIFlashWriteDatas(uint32_t uiAddress, const void *pvBuff, int32_t iLength);
int8_t cSPIFlashReadDatas(uint32_t uiAddress, void *pvBuff, int32_t iLength);
uint32_t uiSPIFlashReadID(void);


#endif /* DEVICESSPIFLASH_H_ */
